﻿using IR.Controller.ModelControllers;
using IR.WPF.UI.Controls;
using IR.WPF.UI.Windows;
using IR.WPF.ViewModels.LandingViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace IR.WPF.UI.ViewModels
{
    public class VMOptionPane : ViewModelBase
    {
        private List<CVersion> _versions;
        private List<CUser> _users;
        private List<CIdea> _ideas;

        private ICommand _backCommand;
        private ICommand _createCommand;
        private ICommand _editCommand;

        /// <summary>
        /// Initializer for the optionPane
        /// </summary>
        /// <param name="typeOfOptionPane">Pass in the viewmodel this is editing</param>
        /// <param name="currentlySelectedController">Pass in the currently selected Controller #</param>
        public VMOptionPane(ViewModelBase typeOfOptionPane, int currentlySelectedController)
        {
            if (typeOfOptionPane.GetType().Equals(typeof(VMVersionHeader)))
            {
                VMVersionHeader versionHeader = typeOfOptionPane as VMVersionHeader;
                List<VMVersion> versions = versionHeader.Versions.ToList();
                _versions = new List<CVersion>(versions.Count);

                foreach (VMVersion versionViewModel in versions)
                {
                    _versions.Add(versionViewModel.VersionController);
                }
            }
            else if (typeOfOptionPane.GetType().Equals(typeof(VMIdeaHeader)))
            {
                VMIdeaHeader ideaHeader = typeOfOptionPane as VMIdeaHeader;
                List<VMIdea> ideas = ideaHeader.Ideas.ToList();
                _ideas = new List<CIdea>(ideas.Count);

                foreach (VMIdea ideaViewModel in ideas)
                {
                    _ideas.Add(ideaViewModel.IdeaController);
                }
            }
            else if (typeOfOptionPane.GetType().Equals(typeof(VMUserHeader)))
            {
                VMUserHeader userHeader = typeOfOptionPane as VMUserHeader;
                List<VMUser> users = userHeader.Users.ToList();
                _users = new List<CUser>(users.Count);

                foreach (VMUser userViewModel in users)
                {
                    _users.Add(userViewModel.UserController);
                }
            }
        }

        #region **Properties**
        #endregion

        #region **Commands**
        /// <summary>
        /// Gets a command defined for Back
        /// </summary>
        public ICommand Back
        {
            get
            {
                if (_backCommand == null)
                    _backCommand = new RelayCommand(p => BackFunction(p as Control), p => CanGoBack(p as Control));

                return _backCommand;
            }
        }

        /// <summary>
        /// Gets a command defined for Create
        /// </summary>
        public ICommand Create
        {
            get
            {
                if (_createCommand == null)
                    _createCommand = new RelayCommand(p => CreateFunction(p as Control), p => CanCreate(p as Control));

                return _createCommand;
            }
        }

        /// <summary>
        /// Gets a command defined for Edit
        /// </summary>
        public ICommand Edit
        {
            get
            {
                if (_editCommand == null)
                    _editCommand = new RelayCommand(p => EditFunction(p as Control), p => CanEdit(p as Control));

                return _editCommand;
            }
        }
        #endregion

        #region **Command Functions**
        protected void BackFunction(Control sender)
        {
            // Get a reference to the Window that contained the sender Control
            Window window = Window.GetWindow(sender);

            window.Close();
        }

        protected void CreateFunction(Control sender)
        {
            if (_versions != null)
            {
                // Create a new window that displays the window
                VMVersionEdit viewModel = new VMVersionEdit();

                // Get a reference to the Window that contained the sender Control
                Window window = Window.GetWindow(sender);

                CompositeControlHost.ShowModal<VersionEdit>(viewModel, window);
            }
            else if (_users != null)
            {
                // Create a new window that displays the window
                VMUserEdit viewModel = new VMUserEdit();

                // Get a reference to the Window that contained the sender Control
                Window window = Window.GetWindow(sender);

                CompositeControlHost.ShowModal<UserEdit>(viewModel, window);
            }
            else if (_ideas != null)
            {
                // Create a new window that displays the window
                VMIdeaEdit viewModel = new VMIdeaEdit();

                // Get a reference to the Window that contained the sender Control
                Window window = Window.GetWindow(sender);

                CompositeControlHost.ShowModal<IdeaEdit>(viewModel, window);
            }
        }

        protected void EditFunction(Control sender)
        {
            MainWindow mainWindow = App.Current.MainWindow as MainWindow;
            if (_versions != null)
            {
                if (mainWindow.CurrentlySelectedVersion != null)
                {
                    // Get the currently selected version and make a view model of it

                    VMVersionEdit viewModel = new VMVersionEdit(mainWindow.CurrentlySelectedVersion.VersionController);

                    // Get a reference to the Window that contained the sender Control
                    Window window = Window.GetWindow(sender);

                    CompositeControlHost.ShowModal<VersionEdit>(viewModel, window);
                }
                else
                {
                    MessageBox.Show("Select a version.");
                }
            }
            if (_users != null)
            {
                if (mainWindow.CurrentlySelectedUser != null)
                {
                    // Get the currently selected version and make a view model of it

                    VMUserEdit viewModel = new VMUserEdit(mainWindow.CurrentlySelectedUser.UserController);

                    // Get a reference to the Window that contained the sender Control
                    Window window = Window.GetWindow(sender);

                    CompositeControlHost.ShowModal<UserEdit>(viewModel, window);
                }
                else
                {
                    MessageBox.Show("Select an user.");
                }
            }
            if (_ideas != null)
            {
                if (mainWindow.CurrentlySelectedIdea != null)
                {
                    // Get the currently selected version and make a view model of it

                    VMIdeaEdit viewModel = new VMIdeaEdit(mainWindow.CurrentlySelectedIdea.IdeaController);

                    // Get a reference to the Window that contained the sender Control
                    Window window = Window.GetWindow(sender);

                    CompositeControlHost.ShowModal<IdeaEdit>(viewModel, window);
                }
                else
                {
                    MessageBox.Show("Select an idea.");
                }
            }
        }
        #endregion

        #region **CanRun Functions**
        protected bool CanGoBack(Control sender)
        {
            return true;
        }

        protected bool CanCreate(Control sender)
        {
            return true;
        }

        protected bool CanEdit(Control sender)
        {
            return true;
        }
        #endregion
    }
}
